package com.example.hzly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hzly.common.Result;
import com.example.hzly.dto.StoryDTO;
import com.example.hzly.entity.Story;
import com.example.hzly.entity.StoryComment;
import com.example.hzly.service.StoryService;
import com.example.hzly.service.StoryFavoriteService;
import com.example.hzly.service.StoryLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stories")
public class StoryController {

    @Autowired
    private StoryService storyService;

    @Autowired
    private StoryFavoriteService storyFavoriteService;

    @Autowired
    private StoryLikeService storyLikeService;

    /**
     * 根据id获取故事详情
     *
     * @param id 故事ID
     * @return StoryDTO
     */
    @GetMapping("/{id}")
    public Result<StoryDTO> getStoryDetail(@PathVariable Long id) {
        return Result.ok(storyService.selectStoryById(id));
    }

    /**
     * 获取故事列表
     *
     * @param current 当前页码
     * @param size    每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/page")
    public Result<IPage<StoryDTO>> getStoryList(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.getStoryList(page));
    }

    /**
     * 根据热度获取故事列表
     *
     * @param current 当前页码
     * @param size    每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/hot")
    public Result<IPage<StoryDTO>> getHotStories(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "6") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.getHotStories(page));
    }

    /**
     * 根据分类获取故事列表
     *
     * @param category 分类名称
     * @param current  当前页码
     * @param size     每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/category/{category}")
    public Result<IPage<StoryDTO>> getStoriesByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.getStoriesByCategory(category, page));
    }

    /**
     * 根据目的地ID获取故事列表
     *
     * @param destinationId 目的地ID
     * @param current       当前页码
     * @param size          每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/destination/{destinationId}")
    public Result<IPage<StoryDTO>> getStoriesByDestination(
            @PathVariable Long destinationId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.getStoriesByDestination(destinationId, page));
    }

    /**
     * 根据作者ID获取故事列表
     *
     * @param authorId 作者ID
     * @param current  当前页码
     * @param size     每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/author/{authorId}")
    public Result<IPage<StoryDTO>> getStoriesByAuthor(
            @PathVariable Long authorId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.getStoriesByAuthor(authorId, page));
    }

    /**
     * 搜索故事
     *
     * @param keyword 关键词
     * @param current 当前页码
     * @param size    每页大小
     * @return IPage<StoryDTO>
     */
    @GetMapping("/search")
    public Result<IPage<StoryDTO>> searchStories(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Story> page = new Page<>(current, size);
        return Result.ok(storyService.searchStories(keyword, page));
    }

    /**
     * 获取所有故事分类
     *
     * @return List<String>
     */
    @GetMapping("/categories")
    public Result<List<String>> getAllCategories() {
        return Result.ok(storyService.getAllCategories());
    }

    /**
     * 获取故事统计信息
     *
     * @return Map<String, Object>
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getStoryStats() {
        return Result.ok(storyService.getStoryStats());
    }

    /**
     * 点赞故事（只能点赞一次）
     */
    @PostMapping("/{id}/like")
    public Result<String> likeStory(@PathVariable Long id, @RequestParam Long userId) {
        boolean ok = storyLikeService.addLike(userId, id);
        return ok ? Result.ok("点赞成功") : Result.error("已点赞");
    }

    /**
     * 取消点赞
     */
    @PostMapping("/{id}/unlike")
    public Result<String> unlikeStory(@PathVariable Long id, @RequestParam Long userId) {
        boolean ok = storyLikeService.removeLike(userId, id);
        return ok ? Result.ok("已取消点赞") : Result.error("未点赞");
    }

    /**
     * 判断是否已点赞
     */
    @GetMapping("/{id}/is-liked")
    public Result<Boolean> isLiked(@PathVariable Long id, @RequestParam Long userId) {
        return Result.ok(storyLikeService.isLiked(userId, id));
    }

    /**
     * 添加故事评论或回复
     */
    @PostMapping("/{id}/comment")
    public Result<Void> addComment(@PathVariable Long id, @RequestBody StoryComment comment) {
        comment.setStoryId(id);
        storyService.addComment(comment);
        return Result.ok();
    }

    /**
     * 删除评论（只能删除自己的评论）
     */
    @DeleteMapping("/comment/{commentId}")
    public Result<Void> deleteComment(@PathVariable Long commentId, @RequestParam Long userId) {
        storyService.deleteComment(commentId, userId);
        return Result.ok();
    }

    /**
     * 获取嵌套评论列表
     */
    @GetMapping("/{id}/comments")
    public Result<List<StoryDTO.CommentDTO>> getComments(@PathVariable Long id) {
        return Result.ok(storyService.getNestedComments(id));
    }

    /**
     * 获取相关故事推荐
     *
     * @param id   故事ID
     * @param size 推荐数量
     * @return List<StoryDTO>
     */
    @GetMapping("/{id}/related")
    public Result<List<StoryDTO>> getRelatedStories(
            @PathVariable Long id,
            @RequestParam(defaultValue = "4") Integer size) {
        return Result.ok(storyService.getRelatedStories(id, size));
    }

    /**
     * 获取最新故事
     *
     * @param size 数量
     * @return List<StoryDTO>
     */
    @GetMapping("/latest")
    public Result<List<StoryDTO>> getLatestStories(@RequestParam(defaultValue = "5") Integer size) {
        return Result.ok(storyService.getLatestStories(size));
    }

    /**
     * 收藏故事
     */
    @PostMapping("/{id}/favorite")
    public Result<String> favoriteStory(@PathVariable Long id, @RequestParam Long userId) {
        boolean ok = storyFavoriteService.addFavorite(userId, id);
        return ok ? Result.ok("收藏成功") : Result.error("已收藏");
    }

    /**
     * 取消收藏
     */
    @PostMapping("/{id}/unfavorite")
    public Result<String> unfavoriteStory(@PathVariable Long id, @RequestParam Long userId) {
        boolean ok = storyFavoriteService.removeFavorite(userId, id);
        return ok ? Result.ok("已取消收藏") : Result.error("未收藏");
    }

    /**
     * 判断是否已收藏
     */
    @GetMapping("/{id}/is-favorite")
    public Result<Boolean> isFavorite(@PathVariable Long id, @RequestParam Long userId) {
        return Result.ok(storyFavoriteService.isFavorite(userId, id));
    }

    /**
     * 发布旅行故事
     */
    @PostMapping("/publish")
    public Result<String> publishStory(@RequestBody Story story) {
        boolean ok = storyService.save(story);
        return ok ? Result.ok("发布成功") : Result.error("发布失败");
    }
}